/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package testing;

import java.util.Properties;
import javax.naming.Context;
import javax.naming.NamingEnumeration;
import javax.naming.directory.Attributes;
import javax.naming.directory.InitialDirContext;
import javax.naming.directory.SearchControls;
import javax.naming.directory.SearchResult;

/**
 *
 * @author satria.sp
 */
public class getData {
    public static void main(String[] args) {
        Properties prop = new Properties();
        String[] attrIDs = {"cn", "distinguishedName", "sn", "givenname", "mail", "telephonenumber", "userPrincipalName"};
        
        try { 
            prop.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory");
            prop.put(Context.SECURITY_AUTHENTICATION, "Simple");
            prop.put(Context.REFERRAL, "follow");
            prop.put(Context.SECURITY_PRINCIPAL, "satria");
            prop.put(Context.SECURITY_CREDENTIALS, "PanjulTE143");
            prop.put(Context.PROVIDER_URL, "ldap://192.168.88.70:389/dc=serper,dc=com");
            
            InitialDirContext context = new InitialDirContext(prop);
            
            SearchControls constraints = new SearchControls();
            constraints.setSearchScope(SearchControls.SUBTREE_SCOPE);
            constraints.setReturningAttributes(attrIDs); 
            NamingEnumeration answer = context.search("", "(objectclass=person)", constraints); 
            
            while (answer.hasMore()) { 
                Attributes attrs = ((SearchResult) answer.next()).getAttributes();
                System.out.println(attrs.get("distinguishedName"));
                System.out.println(attrs.get("givenname")); 
                System.out.println(attrs.get("sn"));
                System.out.println(attrs.get("cn"));
                System.out.println(attrs.get("userPrincipalName"));
                System.out.println(attrs.get("mail"));
                System.out.println(attrs.get("telephonenumber")); 
            } 
        } catch (Exception e) {
        }
    }
}
